﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DepartmentBILL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDepartmentDA departmentDA = DataAccess.CreateDepartmentDA();
        private PubParasInfo pubParasInfo;

        public DepartmentBILL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DepartmentBILL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> departmentIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<DepartmentInfo> departmentListInfo = new List<DepartmentInfo>();
                DepartmentInfo departmentInfo = null;
                for (i = 0; i < departmentIDS.Count; i++)
                {
                    departmentInfo = departmentDA.GetDepartmentByID(dataBase, tran, departmentIDS[i], this.dataBaseID[0], this.pubParasInfo);
                    departmentListInfo.Add(departmentInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (departmentDA.DeleteDepartment(dataBase, tran, departmentIDS, this.dataBaseID) > 0)
                {
                    for (i = 0; i < departmentIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<DepartmentInfo>(dataBase, tran, departmentListInfo[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DEPARTMENT", this.dataBaseID[0]);
                        temp = temp + departmentIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return departmentDA.GetAllDepartment(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string departmentID)
        {
            return departmentDA.GetDepartmentByID(departmentID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            DepartmentQuerEntity departmentQuery = obj as DepartmentQuerEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(departmentQuery.IsActive))
            {
                sqlWhere.Add(" D.IS_ACTIVE " + departmentQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DepartmentID))
            {
                sqlWhere.Add(" D.DEPARTMENT_ID " + departmentQuery.DepartmentID);
            }
            if (!string.IsNullOrEmpty(departmentQuery.Descr))
            {
                sqlWhere.Add(" D.DESCR " + departmentQuery.Descr);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DPFax))
            {
                sqlWhere.Add(" D.DP_FAX " + departmentQuery.DPFax);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DPPhone))
            {
                sqlWhere.Add(" D.DP_PHONE " + departmentQuery.DPPhone);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DPAddress))
            {
                sqlWhere.Add(" D.DP_ADDRESS " + departmentQuery.DPAddress);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DPContact))
            {
                sqlWhere.Add(" D.DP_CONTACT " + departmentQuery.DPContact);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DPContactPhone))
            {
                sqlWhere.Add(" D.DP_CONTACT_PHONE " + departmentQuery.DPContactPhone);
            }
            if (!string.IsNullOrEmpty(departmentQuery.DPContactPhone))
            {
                sqlWhere.Add(" D.DP_CONTACT_PHONE " + departmentQuery.DPContactPhone);
            }
            if (!string.IsNullOrEmpty(departmentQuery.ParentDpID))
            {
                sqlWhere.Add(" D.PARENT_DP_ID " + departmentQuery.ParentDpID);
            }
            if (!string.IsNullOrEmpty(departmentQuery.Remark))
            {
                sqlWhere.Add(" D.REMARK " + departmentQuery.Remark);
            }
            if (!string.IsNullOrEmpty(departmentQuery.CreatedBy))
            {
                sqlWhere.Add(" D.CREATED_BY " + departmentQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(departmentQuery.CreatedDate))
            {
                sqlWhere.Add(departmentQuery.CreatedDate.Replace("@CreatedDate@", " D.CREATED_DATE "));
            }
            return departmentDA.GetDepartmentByQueryList(sqlWhere, departmentQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string departmentID = string.Empty;
            try
            {
                DepartmentInfo departmentInfo = obj as DepartmentInfo;
                if (departmentDA.CheckDepartmentIDUnique(departmentInfo.DepartmentID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2640_001", this.pubParasInfo.UserLanguage, new List<string> { departmentInfo.DepartmentID });
                }
                departmentInfo.CreatedDate = DateTime.Now;
                departmentInfo.UpdatedDate = DateTime.Now;
                departmentInfo.UpdatedBy = departmentInfo.CreatedBy;
                if (departmentDA.InsertDepartment(departmentInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DepartmentInfo>(null, departmentInfo, AuditAction.Insert, this.pubParasInfo.UserID, "DEPARTMENT", this.dataBaseID[0]);
                    departmentID = departmentInfo.DepartmentID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return departmentID;
        }

        public string Update(object obj)
        {
            string departmentID = string.Empty;
            try
            {
                DepartmentInfo departmentInfo = obj as DepartmentInfo;
                departmentInfo.UpdatedDate = DateTime.Now;
                DepartmentInfo oldDepartmentInfo = departmentDA.GetDepartmentByID(departmentInfo.DepartmentID, this.dataBaseID[0], this.pubParasInfo);
                if (departmentDA.UpdateDepartment(departmentInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DepartmentInfo>(oldDepartmentInfo, departmentInfo, AuditAction.Update, this.pubParasInfo.UserID, "DEPARTMENT", this.dataBaseID[0]);
                    departmentID = departmentInfo.DepartmentID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return departmentID;
        }
    }
}

